<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/10/22
 * Time: 13:25
 */

namespace app\common\service\platform_manage\account;


use think\Db;

class ServerUserService
{

    public function findAll($field = '*')
    {
        return Db::name('server_user')->field($field)->select();
    }
    /**
     * @name 查询
     * @param $condition
     * @return array|null|\PDOStatement|string|\think\Model
     * @author wx
     * @date 2019/10/21 16:45
     */
    public function findData($condition)
    {
        return Db::name('server_user')->where($condition)->find();
    }

    /**
     * @name 修改
     * @param $condition
     * @param $updateData
     * @return int|string
     * @author wx
     * @date 2019/10/22 14:18
     */
    public function modify($condition, $updateData)
    {
        return Db::name('server_user')->where($condition)->update($updateData);
    }

    /**
     * @name  后台用户列表
     * @param $params
     * @return array
     * @author wx
     * @date 2019/10/28 15:51
     */
    public function list($params)
    {
        //非维护账号
        $where = [
            ['u.level','<>',0],
        ];

        //按条件查询
        if (isset($params['group_id']) && !empty($params['group_id'])) {
            $where[] = ['ug.group_id','eq',$params['group_id']];
        }

        if (isset($params['host_group_id']) && strlen($params['host_group_id'])) {
            $where[] = ['u.host_group_id','eq',$params['host_group_id']];
        }

        if (isset($params['real_name']) && !empty($params['real_name'])) {
            $where[] = ['u.real_name','like','%'.$params['real_name'].'%'];
        }
        if (isset($params['show_name']) && !empty($params['show_name'])) {
            $where[] = ['u.show_name','like','%'.$params['show_name'].'%'];
        }
        if (isset($params['ctime']) && !empty($params['ctime'])) {
            $ctime_start = strtotime($params['ctime']);
            $where[] = ['u.ctime', 'between', [$ctime_start, $ctime_start + 86399]];
        }

        //邮箱
        if(isset($params['email']) && strlen($params['email'])){
            $where[] = ['u.email','like','%'.$params['email'].'%'];
        }

        if (isset($params['last_login_time']) && !empty($params['last_login_time'])) {
            $last_login_start = strtotime($params['last_login_time']);
            $where[] = ['u.last_login_time', 'between', [$last_login_start,$last_login_start + 86399]];
        }

        //上次登录的IP
        if(isset($params['last_login_ip']) && strlen($params['last_login_ip'])){
            $where[] = ['u.last_login_ip','like','%'.$params['last_login_ip'].'%'];
        }

        //查询字段
        $field = 'u.id,u.show_name,u.real_name,u.ctime,u.last_login_time,u.email,u.last_login_ip,u.host_group_id,hg.name as host_group_name';
        $data = Db::name('server_user')->alias('u')
            ->leftJoin('server_user_group_mapping ug','ug.user_id=u.id') //角色
            ->leftJoin('server_host_group hg','hg.id=u.host_group_id')      //资产组
            ->where($where)->order('u.id desc')->group('u.id')->field($field)->paginate()->toArray();
        return $data;
    }

    /**
     * @name 添加
     * @param $addData
     * @return int|string
     * @author wx
     * @date 2019/10/28 15:52
     */
    public function add($addData)
    {
        return Db::name('server_user')->insert($addData);
    }

    /**
     * @name 删除
     * @param $condition
     * @return int
     * @author wx
     * @date 2019/10/28 16:06
     */
    public function del($condition)
    {
        return Db::name('server_user')->where($condition)->delete();
    }

    /**
     * @name: 通过 id 获取用户详情
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-20 11:21:49
     */
    public function getUserById ($id_a)
    {
        $return = Db::name('ServerUser')->whereIn('id', $id_a)->column('id,show_name,real_name', 'id');

        return $return;
    }



}